package com.hxzy.shop.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 创建人 ： wq
 * 创建时间 ： 2022年05月13日 16:55
 * 功能描述 ： TimeUtils
 */
public class TimeUtils {
    /**
     * 获取+n小时的日期
     *
     * @param past
     * @return
     */
    public static Date getAddDate(Date date, int past) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + past);
        Date today = calendar.getTime();
        return today;
    }


    /**
     * 获取+n天的日期
     *
     * @param past
     * @return
     */
    public static Date getAddDayDate(Date date, int past) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        return today;
    }

    /**
     * string转时间
     */
    public static Date stringToDate(String string) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(string);
        } catch (ParseException e) {
            return new Date();
        }
    }

    /**
     * Date转时间String
     */
    public static String DateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        return time;
    }


//    /**
//     *  获取两个时间之间的间隔天数
//     * @param startTime  开始时间
//     * @param endTime    结束时间
//     * @return      天数          例如2018-11-01 00:00:00至2018-11-30 23:59:59  返回为30
//     */
//    public static Integer getBetweenDays(Date startTime, Date endTime) {
//        int betweenDays = 0;
//        long start = startTime.getTime();
//        long end = endTime.getTime();
//        betweenDays = (int) (Math.abs(end - start)/(24*3600*1000));
//        return betweenDays + 1;
//    }


    /**
     * 获取两个时间之间的间隔天数
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 天数          例如2018-11-01 00:00:00至2018-11-30 23:59:59  返回为30
     */
    public static int getIntervalDays(Date startTime, Date endTime) {
        if (startTime != null && endTime != null) {
            long et = endTime.getTime();
            long st = startTime.getTime();
            long l = et - st;
            long dayCount = l / (24 * 60 * 60 * 1000);
            return new Integer((int) (dayCount + 1));
        }
        return -10;
    }

    /**
     * 获取+n年+n月+日的日期
     * @return
     */
    public static Date getByAddDate(Date date, int year, int month, int day) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //年
        if (year>0) {
            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + year);
        }
        //月
        if (month>0) {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + month);
        }
        //日
        if (day>0) {
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + day);
        }
        Date today = calendar.getTime();
        return today;
    }

    /**
     * 获取+n天+n小时+n分钟的日期
     * @return
     */
    public static Date addDateDay(Date date, int day, int hour, int minute) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        //日
        if (day>0) {
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + day);
        }
        //年
        if (hour>0) {
            calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + hour);
        }
        //月
        if (minute>0) {
            calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + minute);
        }
        Date today = calendar.getTime();
        return today;
    }

    /**
     * 比较时间大小
     * @return
     */
    public static Date toCompare(int month, int day)  throws ParseException {
        SimpleDateFormat sdf  =new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        Date date = sdf.parse(year+"-"+month+"-"+day);
        if(date.getTime() >= today.getTime()){
            return date;
        }
        return getByAddDate(today,1,0,0 );
    }

    public static void main(String[] args) throws ParseException{

        int month=8;
        int day=25;
        SimpleDateFormat sdf  =new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        Date date = sdf.parse(year+"-"+month+"-"+day);
        if(date.getTime() >= today.getTime()){
            System.out.println("x:"+date);
        }else {
            System.out.println("d:"+getByAddDate(date,1,0,0 ));

        }

    }
}
